package com.changli02.tcpudp;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest3 {
    @Test
    public void client() {
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream os = null;
        InputStream is = null;
        try {
            InetAddress inet = InetAddress.getByName("10.151.166.121");
            int port = 8080;
//          客户端
            socket = new Socket(inet, port);
            //创建File、流
            File file = new File("D:\\后端\\File_ioStream\\home.jpg");

            fis = new FileInputStream(file);

            os = socket.getOutputStream();


            byte[] buf = new byte[1024];
            int len ;
            while ((len = fis.read(buf)) !=-1){
                os.write(buf,0,len);
            }
            System.out.println("数据发送完毕....");
            socket.shutdownOutput();
            //接收服务端的消息
            is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len1;
            byte[] buf1 = new byte[1024];
            while ((len1 = is.read(buf1)) != -1){
                baos.write(buf1,0,len1);
            }
            System.out.println(baos.toString());


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关流
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


    @Test
    public void server() {
        ServerSocket ss = null;
        Socket ac = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            //服务端 ServerSocket
            ss = new ServerSocket(8080);

            ac = ss.accept();


            is = ac.getInputStream();

            File file = new File("D:\\后端\\File_ioStream\\server-home_copy1.jpg");

            fos = new FileOutputStream(file);

            byte [] buf = new byte[1024];

            int len ;
            while((len = is.read(buf))!=-1){
                fos.write(buf,0,len);
            }
            System.out.println("数据接收完毕......");

            os = ac.getOutputStream();
            os.write("你发送的图片很漂亮，我已经收到了".getBytes());


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos !=null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is !=null)

                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ac !=null)

                    ac.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss !=null)

                    ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }
}
